package lion.http;

import java.util.concurrent.Executor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

public class Netty4HttpServer {

	private static Logger logger = LoggerFactory.getLogger(Netty4HttpServer.class);

	private int port;
	
	private EventLoopGroup bossGroup;

	private EventLoopGroup workerGroup;

	private Channel serverChannel;
	
	public Netty4HttpServer(int p_port) {
		this.port = p_port;
	}

	public void start(Executor threadExecutor, final ChannelReadHttpHandler channelReadHandler) throws Exception {
		int bossThreadCount = 0x1;
		int workerThreadCount = 0x1;
		if (threadExecutor != null) {
			bossGroup = new NioEventLoopGroup(bossThreadCount, threadExecutor); // (1)
			workerGroup = new NioEventLoopGroup(workerThreadCount, threadExecutor);
		} else {
			bossGroup = new NioEventLoopGroup(bossThreadCount); // (1)
			workerGroup = new NioEventLoopGroup(workerThreadCount);
		}
		ServerBootstrap b = new ServerBootstrap(); // (2)
		b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
				.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						// server端发送的是httpResponse，所以要使用HttpResponseEncoder进行编码
						ch.pipeline().addLast(new HttpResponseEncoder());
						// server端接收到的是httpRequest，所以要使用HttpRequestDecoder进行解码
						ch.pipeline().addLast(new HttpRequestDecoder());
						ch.pipeline().addLast(new HttpObjectAggregator(512 * 1024));
						// ch.pipeline().addLast(new ChunkedWriteHandler());
						ch.pipeline().addLast(new HttpServerInboundHandler(channelReadHandler));
					}
				}).option(ChannelOption.SO_BACKLOG, 128) // (5)
				.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
		serverChannel = b.bind(port).sync().channel();
		logger.warn("Netty4 HttpServer listening on port:{},boss={},worker={} successfully!", port,bossThreadCount,workerThreadCount);
	}

	public static void main(String[] args) throws Exception {
		Netty4HttpServer server = new Netty4HttpServer(8000);
		server.start(null, new HttpStrHandler("gm") {
			@Override
			public String strHttpHandle(ChannelHandlerContext ctx, FullHttpRequest request, String content)
					throws Exception {
				String request_uri = request.uri();
				logger.info("http request,uri={}", request_uri);
				if (request_uri.startsWith("/gm/query1")) {
					String myName = "huijige灰机哥";
					int xx = 0;
					int h = 50 / xx;
					return myName;
				} else {
					String myName = "上海观趣guanqu";
					return myName;
				}
			}
		});
	}

	public void shutdown() {
		try {
			// Wait until the server socket is closed.
			// In this example, this does not happen, but you can do that to
			// gracefully
			// shut down your server.
			serverChannel.close().sync();
		} catch (InterruptedException e) {
			logger.error("", e);
		}
		workerGroup.shutdownGracefully();
		bossGroup.shutdownGracefully();
	}

}
